
var x2=0;
var flagState=0;

$(document).ready(function(){
//	alert("돼냐?");
	
selectPencil();

	$("#stopBtn2").on("click",function(){
		x2=0;
	});
	
		$("#sendBtn2").on("click",function(){
			
			$("#reportbtn1").css({"display":"none"});
			if(x2>=1){
					alert("작동중입니다."+x2);
			}
			if(x2==0){
				alert("작동합니다."+x2);
				x2=1;
				
			
				$.ajax({
						"url":"/pencil_factory/insertMachine/begin.do",
						"type":"post",
						"data":{"orderNumS":$("#orderNumS2").val()},
						"dataType":"json",
						"beforeSend":function(){
							
						},
						"success":function(data){//변수 data의 type- javascript 객첵}
							
							
						},"error":function(xhr, status, msg){
						//	alert("오류 발생 :"+xhr.readyState+","+status+","+msg);
					}
				});
			
				
				
				
			
				state_machine(1);
				pullServer();
				$("#db_area12").empty();
				$("#db_area12").append("<b>작업을 시작합니다.</b><br>");
			
			}
		});
		
		////////////////
		
	$("#testBtn").on("click",function(){
		alert("퍼센트 테스트:"+valuePersent($("#test").val()));
	});
		
		
	state_machine(flagState);
	$("#reportbtn2").click(function(){
		$.ajax({
			"url":"/pencil_factory/insertMachine/getReport.do",
			"type":"post",

			"dataType":"json",
		
				"success":function(data){
					if(reCount==0){
						$("#list").empty();
						for(var i = 0; i<data.length;i++){
							$("#list").prepend("<b>"+
									"흑심 양 : " + data[i].blackStick+
									"&nbsp;&nbsp;나무판 양 : " + data[i].holeBoard +
									"&nbsp;&nbsp;시작 시간 : " + data[i].startTime + 
									"&nbsp;&nbsp;끝난 시간 : " + data[i].endTime + 
									"&nbsp;&nbsp;정상 품   : " + data[i].normal +
									"&nbsp;&nbsp;비정상 품 : " + data[i].abnormal +
									"&nbsp;&nbsp;상태 : " + data[i].state +
									"&nbsp;&nbsp;기계피로도 : " + data[i].machineLife +
									
									"</b><br>");
						}
					}
					
					
				}
		
		});

		 $("#dialog_layer").dialog({ "width":600,
	            "Height": 500,"modal":true,"position":{"my":"center top",
				"at" : "bottom",
				"of" : "#input_area"}});		
		 
	});
});

function test(){
	alert("천재");
}
var reCount = 0;

var endflag=0;
var steprun=0;//진행 상태를 말해준다.
var products=0;//완성품의 수 카운터
var count = 0;
var firstBs = 0;
var firstNp = 0;
function pullServer(){
	
	//alert("천재");
	$.ajax({
		"url":"/pencil_factory/insertMachine/getmoniser.do",
		"type":"post",
		"data":{"id":$("#cid").val()},
		"dataType":"json",
		"beforeSend":function(){
				data=null;
				$("#oderNum21").html(""+$("#orderNumS2").val()+"");
				
			},
			"success":function(data){
				
				if(count==0){
					firstBs = data.blackStick;
					firstNp = data.holeBoard;
					$("#firstBs").html("<b>"+firstBs+"</b>");
					$("#firstNp").html("<b>"+firstNp+"</b>");
					
					count++;
				}			
				steprun=data.runstep;
				products=(data.normal+data.abnormal)*5;
				
				$("#d52, #sTime2, #eTime2, #product2").empty();
				$("#d52").html("<b>"+data.runstep+"</b>");
				$("#d52").css({"width":""+data.runstep+"%"});
				
				steprun=data.runstep;
				
				$("#tg12").html("<b>"+(data.blackStick-5)+"</b>");
				$("#tg22").html("<b>"+(data.holeBoard-2)+"</b>");
				$("#tg42").html("<b>"+data.machineLife+"</b>");
			//	alert(data.state);
				
				if(data.state == '불량'){
					$("#db_area12").prepend("<b>불량품이 발생했습니다.-불량품량:"+data.abnormal+"</b><br>");
					
					flagState=2;
					//alert(flagState);
				}
			
					if(data.state == '정상'){
						$("#db_area12").prepend("<font size='3'>정상품:"+data.normal+", 상태:"+data.state+", 시간:"+data.endTime+"</font><br>");
						
						flagState=1;
					//	alert(flagState);
					}
			
				$("#sTime2").html("<font size='1'>"+data.startTime+"</font>");
				$("#eTime2").html("<font size='1'>"+data.endTime+"</font>");
				$("#product2").html("<b>"+(data.normal*5)+"</b>");
						
			},"error":function(xhr, status, msg){
				alert("오류 발생 :"+xhr.readyState+","+status+","+msg);
		}		
	});

	//////
	state_machine(flagState);

/////////////////////////////////////////////////////////////	
 
	endflag=endflag+1;//끝을 위한 플러그 첫번째 값이 쓰레기 값이라 처리에 어려움이 있어 넣었다.
	if(endflag >2){
		selectPencil();
		if($("#orderNumS2").val()==products){
		//if(steprun >= 100){
			//alert("완료");
		//	$("#db_area").prepend("<b>작업이 완료되었습니다.</b><br>");
				x2=0;
				endflag=0;
				steprun=0;
				products=0;
				flagState=0;
				state_machine(flagState);
				$("#reportbtn1").css({"display":"block"});
				//////////////////////////////끝나는 부분
				//alert("종료한다.");
				
					$("#db_area12").prepend("<b>작업이 완료되었습니다.</b><br>");
				//$("#db_area").children(1).removeChild();
				//$("#db_area").lastChild().removeChild();
				
			return false();
		}
		
	}
///////////////////////////////////////////////////////////////////	
	setTimeout("pullServer()",1300);
}
	
function state_machine(flagState){
	if(flagState==0){
		$("#st_box32").css({"background-color":"#ff0000"});	
		$("#st_box12, #st_box22").css({"background-color":"#000042"});	
		
	}
	if(flagState == 1){
		$("#st_box12").css({"background-color":"#ABF200"});	
		$("#st_box22, #st_box32").css({"background-color":"#000042"});	
	}
	if(flagState == 2){
		$("#st_box22").css({"background-color":"#FFFF48"});	
		$("#st_box12, #st_box32").css({"background-color":" #000042"});	
	}
}

function test1(a){
	alert("test"+a);
}
	/////////////////////
		//받아 오는 값을 퍼센트로 처리 
		var a1=0;
		function valuePersent(met){
			if(a1==0){
				a1=met;
				alert(a1);
			}
			if(a1>0){
			var	b1=a1/100;
			return persent=met/b1;
			}
}		

function selectPencil(){
			$.ajax({
				"url":"/pencil_factory/insertMachine/getPencil.do",
				"type":"post",

				"dataType":"json",
			
					"success":function(data){
						$("#db_pencil").html("<b>"+data.count+"</b>");
						
					}
			
			});
			
 }